package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDaoa;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class RoleserviceImpl implements  Roleservice {
//增
    @Autowired
    RoleDaoa roledaoa;
    @Transactional
    public void add (Integer[] mids, Integer[] pids, Role role) {
             roledaoa.add(role);
        if(role.getId() != null && mids != null && mids.length > 0){
            for (Integer mid : mids) {
                roledaoa.setm(role.getId() ,mid );
            }
        }
        if(role.getId() != null && pids != null && pids.length > 0){
            for (Integer pid : pids) {
                roledaoa.setp(role.getId() ,pid );
            }
        }
    }
//查所有
    @Override
    public List<Permission>  findAllp() {
        return roledaoa.findAllp();
    }

    @Override
    public List<Menu>  findAllm() {
        return roledaoa.findAllm();
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = roledaoa.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page);
    }

    @Override
    @Transactional
    public void delById(Integer id) {
        //删除关系
        roledaoa.delmr(id);
        roledaoa.delpr(id);
        //删除检查组
      roledaoa.delById(id);
    }
//改的回显
    @Override
    public List<Integer> findmById(Integer id) {
        return roledaoa.findmById(id);
    }

    @Override
    public List<Integer> findpById(Integer id) {
        return roledaoa.findpById(id);
    }

    @Override
    public Role findById(Integer id) {
        return roledaoa.findById(id);
    }


    //改
    @Override
    @Transactional
    public void edit(Integer[] mids, Integer[] pids, Role role) {
        roledaoa.edit(role);

        roledaoa.delmr(role.getId());
        roledaoa.delpr(role.getId());


        if (role.getId() != null && mids != null && mids.length > 0) {
            for (Integer mid : mids) {
               roledaoa.setm(role.getId(),  mid );
            }
        }
        if (role.getId() != null && pids != null && pids.length > 0) {
            for (Integer pid : pids) {
                roledaoa.setp(role.getId(), pid);
            }
        }
    }

    @Override
    public List<Role> findAll() {
        return roledaoa.findall();
    }


}



